<?php
/**
 * Onde será executado o evento
 *
 */
class Runat
{
	const Client = 'client';
	const Server = 'server';
}

class EventType
{
	const Click = 'click';
	const DbClick = 'dblclick';	
	const Change = 'change';	
}

class EffectType
{
	const Show = 'show';
	const Hide = 'hide';	
	const Toggle = 'toggle';	
	const FadeIn = 'fadeIn';	
	const FadeOut = 'fadeOut';	
}

Class EffectSpeed
{
	const Slow = 'slow';
	const Normal = 'normal';	
	const Fast = 'fast';	
}

class Event
{
	/**
	 * Tipo do evento
	 *
	 * @var string
	 */
	public $type;
	
	/**
	 * Modelo
	 *
	 * @var string
	 */
	public $module;
	
	/**
	 * Função a ser chamada
	 *
	 * @var string
	 */
	public $funct;
	/**
	 * Parametros para função
	 *
	 * @var array
	 */
	public $args;
	/**
	 * Onde será executado
	 *
	 * @var Runat
	 */
	public $runat;
	/**
	 * Objeto alvo
	 *
	 * @var XmlControl
	 */
	public $target;
	
	
	/**
	 * Id do objeto que gerou evento
	 *
	 * @var string
	 */
	public $_objectid;
	
	public function Event($type, $funct, $args, $runat, $id)
	{
		$func =  $this->getFunctionParams($funct);
		$this->type = $type;
		$this->module = $func[1];
		$this->args = $args;
		$this->runat = $runat;
		$this->funct = $this->getEncodedFunction($func[0]);
		$this->_objectid = $id;
	}
	
	
	public function bind($node)
	{
		$nodeEvent = XmlUtil::CreateChild($node, 'event');
		XmlUtil::AddAttribute($nodeEvent, "type", $this->type);
		XmlUtil::AddAttribute($nodeEvent, "module", $this->module);
		XmlUtil::AddAttribute($nodeEvent, "function",$this->funct);
		XmlUtil::AddAttribute($nodeEvent, 'runat', $this->runat);
		XmlUtil::AddAttribute($nodeEvent, 'id', $this->_objectid);
	}
	
	public static function getEncodedFunction($funct)
	{
		return base64_encode($funct);
	}
	
	private function getFunctionParams($funct)
	{
		$arrParts = explode('.', $funct);
		$funct = array_pop($arrParts);
		$module = implode('.', $arrParts);
		return array($funct, $module);
	}	
	
	private function configCallFunction($funct, $module = '')
	{
		$funct = self::getEncodedFunction($funct);
		return "return Call.callEvent('$funct', '$module',event, this, '$this->runat')";
	}
}
?>